package com.butnet.myframe.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestParamterFilter implements Filter {
	private String name = null;
	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
		String value = arg0.getParameter(name);
		if(value!=null&&(value.trim().equalsIgnoreCase("true")||value.trim().equalsIgnoreCase("yes"))){
			MyHttpServletRequest req = new MyHttpServletRequest((HttpServletRequest)arg0);
			req.getParameterMap().put(this.name, new String[]{"false"});
			((HttpServletResponse)arg1).setCharacterEncoding("GBK");
			arg2.doFilter(req, arg1);
		}else{
			arg2.doFilter(arg0, arg1);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		String value = arg0.getInitParameter("paramterName");
		if(value!=null&&value.trim().length()!=0)
			name = value.trim();
	}
}
